package com.tanhua.app.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.github.tobato.fastdfs.FdfsClientConfig;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.api.db.UserInfoApi;
import com.tanhua.api.mongo.VideoApi;
import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.autoconfig.template.OssTemplate;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.FollowUser;
import com.tanhua.domain.mongo.Video;
import com.tanhua.domain.vo.PageBeanVo;
import com.tanhua.domain.vo.VideoVo;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.remoting.Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.server.WebServer;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import javax.jws.WebService;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class VideoService {

    @Reference
    private VideoApi videoApi;

    @Reference
    private UserInfoApi userInfoApi;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private OssTemplate ossTemplate;
    @Autowired
    private FastFileStorageClient client;
    @Autowired
    private FdfsWebServer webServer;



    public PageBeanVo findByPage(Integer pageNum, Integer pageSize) {  // 第一页  第二页  第三页

        // 1、调用aip完成视频分页查询
        PageBeanVo pageBeanVo = videoApi.findByPage(pageNum, pageSize);

        // 2、封装vo
        List<VideoVo> voList = new ArrayList<>();
        List<Video> videoList = (List<Video>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(videoList)) {
            for (Video video : videoList) {
                VideoVo vo = new VideoVo();
                UserInfo userInfo = userInfoApi.fingByUserId(video.getUserId());
                BeanUtil.copyProperties(userInfo, vo);
                BeanUtil.copyProperties(video, vo);
                vo.setCover(video.getPicUrl()); // 小视频封面
                vo.setSignature(video.getText()); // 小视频签名
                vo.setHasLiked(0); // 是否点赞

                if (stringRedisTemplate.hasKey(StrUtil.format("follow_user_{}_{}", UserHolder.getUserId(), video.getUserId()))) {
                    vo.setHasFocus(1); // 是否关注
                } else {
                    vo.setHasFocus(0); // 是否关注
                }

                vo.setId(video.getId().toHexString()); // 小视频id

                voList.add(vo);
            }
        }

        pageBeanVo.setItems(voList);
        return pageBeanVo;
    }

    public ResponseEntity saveVideo(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        // 1、封面上传到阿里云oss
        String picUrl = ossTemplate.upload(videoThumbnail.getOriginalFilename(), videoThumbnail.getInputStream());
        // 2、小视频上传fastDFS
        StorePath storePath = client.uploadFile(videoFile.getInputStream(), videoFile.getSize(), FileUtil.extName(videoFile.getOriginalFilename()), null);
        String videoUrl = webServer.getWebServerUrl() + storePath.getFullPath();
        // 3、创建video对象
        Video video = new Video();
        video.setUserId(UserHolder.getUserId()); // 发布人
        video.setPicUrl(picUrl); // 封面
        video.setVideoUrl(videoUrl); // 小视频
        video.setText("左右右手一个慢动作~~~"); // 文字
        // 4、保存
        videoApi.save(video);

        return ResponseEntity.ok(null);
    }

    public ResponseEntity userFocus(Long followUserId) {
        // 1、封装 FollowUser对象
        FollowUser followUser = new FollowUser();
        followUser.setUserId(UserHolder.getUserId()); // 登录人id
        followUser.setFollowUserId(followUserId); // 小视频用户id
        // 2、调用api保存
        videoApi.saveFollowUser(followUser);

        // 3、向redis中存储关注标记
        stringRedisTemplate.opsForValue().set(StrUtil.format("follow_user_{}_{}", UserHolder.getUserId(), followUserId), "1");

        return ResponseEntity.ok(null);
    }

    public ResponseEntity userUnFocus(Long followUserId) {
        // 1、调用api删除
        videoApi.removeFollowUser(UserHolder.getUserId(), followUserId);

        // 2、从redis中删除关注标记
        stringRedisTemplate.delete(StrUtil.format("follow_user_{}_{}", UserHolder.getUserId(), followUserId));
        return ResponseEntity.ok(null);
    }
}